﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorldConqueror.Libraries.Maths.Enumerations;
using WorldConqueror.Libraries.Maths.Factories;
using WorldConqueror.Libraries.Maths.MathShapes;

namespace WorldConqueror.Libraries.Maths.Functions.Distance
{
    public interface IPolylineLength : IOneParameterFunction<IMathSphericalPolyline, double>
    {
    }

    public class PolylineLength : IPolylineLength
    {
        internal IFunctionsFactory FunctionsFactory
        {
            get;
            private set;
        }

        public PolylineLength(
            IMathSphericalPolyline mathSphericalPolyline,
            IFunctionsFactory functionsFactory)
        {
            MathShape = mathSphericalPolyline;
            FunctionsFactory = functionsFactory;
        }

        public IMathSphericalPolyline MathShape
        {
            get;
            private set;
        }

        public double Calculate()
        {
            var initialCoordinate = MathShape.MathCoordinates.First();
            
            foreach (var coordinate in MathShape.MathCoordinates)
            {
                var distanceFunction = FunctionsFactory.CreateOneParameterFunction<double>(
                    FunctionType.PolylineLength, 
                    MathShape);

                MathShape.Perimeter += distanceFunction.Calculate();

                initialCoordinate = coordinate;
            }

            return MathShape.Perimeter;
        }
    }
}
